class ListNode:
    def __init__(self, x,next=None):
        self.val = x
        self.next = next

    def printMe(self):
        import sys
        sys.stdout.write(str(self.val)+'->')
        next = self.next;
        if next:
            next.printMe()
        else:
            print 'None'

class Solution:
    # @param head, a ListNode
    # @param val, an integer
    # @return a ListNode
    def removeElements(self, head, val):
        # Write your code here
        rtn = head
        if head:
            if head.val == val:
                rtn = self.removeElements(head.next,val)
            else:
                head.next = self.removeElements(head.next,val);
                            
        return rtn

if __name__=='__main__':
    head = ListNode(1,ListNode(2,ListNode(3,ListNode(3,ListNode(4,(ListNode(5,ListNode(3,ListNode(3,None)))))))))
    head.printMe()
#    print head.val
    solution = Solution();
    result = solution.removeElements(head,3);
    result.printMe();
